package com.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.entity.ContactNote;
import com.util.RowMapper;

public class ContactNoteDao {
	
	com.util.DBHelp<com.entity.ContactNote> db = new com.util.DBHelp<com.entity.ContactNote>();
	
	private class ContactNoteRowMapper implements RowMapper<ContactNote>{
		
		public ContactNote mapperRow(ResultSet rs) throws SQLException {
			ContactNote cn = new ContactNote();
			cn.setId(rs.getString("id"));
			cn.setContent(rs.getString("content"));
			cn.setCreatetime(rs.getString("createtime"));
			cn.setContactid(rs.getString("contactid"));
			cn.setEmployeeid(rs.getString("employeeid"));
			
			return cn;
		}
		
	}
	
	public void addnote(ContactNote cnote) {
		String sql = "INSERT INTO `t_contactnote`(`id`,`content`,`createtime`,`contactid`,`employeeid`)VALUES(?,?,?,?,?);";
		//int line = db.executeSQL(sql,cnote.getId(),cnote.getContent(),cnote.getCreatetime(),cnote.getContactid(),cnote.getEmployeeid());
		db.executeSQL(sql,cnote.getId(),cnote.getContent(),cnote.getCreatetime(),cnote.getContactid(),cnote.getEmployeeid());
	}

	public List<ContactNote> findAll() {
		
		String sql = "SELECT `id`,`content`,`createtime`,`contactid`,`employeeid` FROM`t_contactnote` ORDER BY `createtime` DESC";
		return db.executeQueryForList(sql, new ContactNoteRowMapper());
	}

	public List<ContactNote> getByCid(String id) {
		String sql="SELECT `id`,`content`,`createtime`,`contactid`,`employeeid` FROM `t_contactnote` WHERE `contactid`=? ORDER BY `createtime` DESC";
		return db.executeQueryForList(sql, new ContactNoteRowMapper(), id);
	}

	public void deleteByCId(String id) {
		String sql="DELETE FROM `t_contactnote` WHERE `contactid`=?";
		db.executeSQL(sql, id);
	}
	
}
